package org.springblade.modules.platform.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springblade.modules.platform.dto.MeasuringPlatformConfigDTO;
import org.springblade.modules.platform.dto.MeasuringPlatformTypeDTO;
import org.springblade.modules.platform.entity.DeviceTable;
import org.springblade.modules.platform.entity.MeasuringPlatformConfig;
import org.springblade.modules.platform.entity.MeasuringPlatformType;
import org.springblade.modules.platform.service.IDeviceTableService;
import org.springblade.modules.platform.service.IMeasuringPlatformConfigService;
import org.springblade.modules.platform.service.IMeasuringPlatformTypeService;
import org.springblade.modules.platform.vo.MeasuringPlatformConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SYG
 * @since 2023-06-29
 */


@RestController
@AllArgsConstructor
@RequestMapping("/service-huateng-sxdb/measuringPlatformTable")
@Api(value = "", tags = "新版本测台管理")
@Slf4j
public class MeasuringPlatformTypeController {


    private final IMeasuringPlatformTypeService iMeasuringPlatformTypeService;
    private final IDeviceTableService iDeviceTableService;
    private final IMeasuringPlatformConfigService iMeasuringPlatformConfigService;
    @PostMapping(value = "/install")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增", notes = "传入deptInfoTableDTO")
    public R add(@RequestBody MeasuringPlatformTypeDTO measuringPlatformTypeDTO){
        iMeasuringPlatformTypeService.insertMeasuringPlatformType(measuringPlatformTypeDTO);



        return R.success("成功");
    }
    @PostMapping(value = "/algorithmConfiguration")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增正向逆向算法配置", notes = "传入deptInfoTableDTO")
    public R algorithmConfiguration(@RequestBody MeasuringPlatformConfigDTO measuringPlatformTypeDTO){
        MeasuringPlatformConfig copy1 = BeanUtil.copy(measuringPlatformTypeDTO, MeasuringPlatformConfig.class);
        iMeasuringPlatformConfigService.save(copy1);
        return R.success("成功");
    }
    @PutMapping(value = "/updateAlgorithmConfiguration")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "编辑正向逆向算法配置", notes = "传入deptInfoTableDTO")
    public R updateAlgorithmConfiguration(@RequestBody MeasuringPlatformConfigDTO measuringPlatformTypeDTO) {
        iMeasuringPlatformConfigService.removeById(measuringPlatformTypeDTO.getId());
        if(measuringPlatformTypeDTO.getxType()==null
                &&measuringPlatformTypeDTO.getyType()==null
                &&measuringPlatformTypeDTO.getzType()==null
                &&measuringPlatformTypeDTO.getAlphaType()==null
                &&measuringPlatformTypeDTO.getBetaType()==null
                &&measuringPlatformTypeDTO.getGamaType()==null){
            return R.success("成功");
        }
        MeasuringPlatformConfig copy1 = BeanUtil.copy(measuringPlatformTypeDTO, MeasuringPlatformConfig.class);
        iMeasuringPlatformConfigService.save(copy1);
        return R.success("成功");
    }
    @GetMapping(value = "/algorithmConfiguration/{measuringPlatformId}/{platformTypeCode}")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "正向逆向算法配置详情", notes = "传入deptInfoTableDTO")
    public R<MeasuringPlatformConfigVO> algorithmConfiguration(@PathVariable Long measuringPlatformId,
                                                               @PathVariable Integer platformTypeCode) {
        MeasuringPlatformConfig one = iMeasuringPlatformConfigService.getOne(Wrappers.<MeasuringPlatformConfig>query().lambda()
                .eq(MeasuringPlatformConfig::getMeasuringPlatformId, measuringPlatformId)
                .eq(MeasuringPlatformConfig::getPlatformTypeCode, platformTypeCode));
        if(one==null){
            return R.data(null);
        }
        MeasuringPlatformConfigVO copy1 = BeanUtil.copy(one, MeasuringPlatformConfigVO.class);
        return R.data(copy1);
    }
    @PutMapping(value = "/update")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "编辑", notes = "传入deptInfoTableDTO")
    public R edit(@RequestBody MeasuringPlatformTypeDTO measuringPlatformTypeDTO) {
        iMeasuringPlatformTypeService.updateMeasuringPlatformType(measuringPlatformTypeDTO);
        return R.success("成功");
    }
    @GetMapping(value = "/details/{id}")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入deptInfoTableDTO")
    public R details(@PathVariable Long id) {
        return R.data(iMeasuringPlatformTypeService.deileMeasuringPlatformType2(id));
    }
    @PutMapping(value = "/delete/{id}")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "删除", notes = "传入deptInfoTableDTO")
    public R delete(@PathVariable Long id) {
        iMeasuringPlatformTypeService.deleteMeasuringPlatformType(id);
        return R.success("成功");
    }
    @GetMapping("/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "侧台列表", notes = "传入deviceTablePageDTO")
    public R<IPage<MeasuringPlatformType>> list(MeasuringPlatformType measuringPlatformTypeDTO, Query query) {
        measuringPlatformTypeDTO.setIsDeleted(0);
        IPage<MeasuringPlatformType> page = iMeasuringPlatformTypeService.page(Condition.getPage(query), Condition.getQueryWrapper(measuringPlatformTypeDTO));
        return R.data(page);
    }


    @GetMapping("/deviceTablelist")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "设备下拉框", notes = "传入deviceTablePageDTO")
    public R<List<DeviceTable>> list() {
        List<MeasuringPlatformType> list = iMeasuringPlatformTypeService.list(Wrappers.<MeasuringPlatformType>query().lambda().eq(MeasuringPlatformType::getIsDeleted, 0));
        if(Func.isEmpty(list)){
            List<DeviceTable> list1 = iDeviceTableService.list(Wrappers.<DeviceTable>query().lambda().eq(DeviceTable::getIsDeleted, 0));
            return R.data(list1);
        }else {
            List<String> collect = new ArrayList<>();
            list.forEach(x->collect.addAll(Arrays.asList(x.getDeviceId().split(","))));
            List<DeviceTable> list1 = iDeviceTableService.list(Wrappers.<DeviceTable>query().lambda().eq(DeviceTable::getIsDeleted, 0)
                    .eq(DeviceTable::getDeviceType, 1)
                    .notIn(DeviceTable::getDeviceId,collect));
            return R.data(list1);
        }

    }
    @GetMapping("/highDeviceTablelist")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取高精度设备下拉框", notes = "传入deviceTablePageDTO")
    public R<List<DeviceTable>> highDeviceTablelist() {
        List<DeviceTable> list1 = iDeviceTableService.list(Wrappers.<DeviceTable>query().lambda().eq(DeviceTable::getIsDeleted, 0)
                .eq(DeviceTable::getDeviceType, 2));
        return R.data(list1);
    }


	@GetMapping("/ht-acquisition")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "设备下拉框", notes = "传入deviceTablePageDTO")
	public R<List<DeviceTable>> htAcquisition() {
		List<DeviceTable> list1 = iDeviceTableService.list(Wrappers.<DeviceTable>query().lambda()
			.in(DeviceTable::getProductId, "HT-ACQUISITION","HT-PERPENDICULOMETER")
			.eq(DeviceTable::getIsDeleted, 0));
		return R.data(list1);

	}
}

